/*
 * QLabelAdapter.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QLabelAdapter.h"

// QLabelAdapter :: Public Constructor. ----------------------------------------

/*!
 * \brief Constructs new QLabelAdapter object.
 */
QLabelAdapter::QLabelAdapter()
  : QLabel()
{
  // empty constructor body
}

// QLabelAdapter :: Public Member Functions. -----------------------------------

/*!
 * \brief Returns icon.
 *
 * \return icon
 */
QIcon QLabelAdapter::icon() const
{
  return m_icon;
}

/*!
 * \brief Sets icon.
 *
 * \param icon the icon
 */
void QLabelAdapter::setIcon(const QIcon &icon)
{
  m_icon = icon;
}

/*!
 * \brief Sets icon size.
 *
 * \param size the size
 */
void QLabelAdapter::setIconSize(const QSize &size)
{
  m_iconSize = size;
  if (alignment() & Qt::AlignHorizontal_Mask) {
    setIndent(size.width() + 1);
  } else {
    setIndent(size.height() + 1);
  }
}

// QLabelAdapter :: Overridden Member Functions. -------------------------------

/*!
 * \inheritDoc
 */
QSize QLabelAdapter::sizeHint()const
{
  QSize hintSize = m_iconSize;
  if (!text().isEmpty()) {
    QSize textSize = QLabel::sizeHint();
    if (alignment() & Qt::AlignHorizontal_Mask) {
      if (textSize.height() > hintSize.height()) {
        hintSize.setHeight(textSize.height());
      }
      hintSize.setWidth(textSize.width() + hintSize.width() + 1);
    } else {
      if (textSize.width() > hintSize.width()) {
        hintSize.setWidth(textSize.width());
      }
      hintSize.setHeight(textSize.height() + hintSize.height() + 1);
    }
  }
  return hintSize;
}

/*!
 * \inheritDoc
 */
void QLabelAdapter::paintEvent(QPaintEvent *e)
{
  if (!m_icon.isNull()) {
    // TODO: reimplement, add calc, alignment
    QStylePainter painter(this);
    QRect cr = contentsRect();
    if (isEnabled()) {
      painter.drawItemPixmap(cr, alignment(), m_icon.pixmap(0, 0,
                                                            QIcon::Normal));
    } else {
      painter.drawItemPixmap(cr, alignment(), m_icon.pixmap(0, 0,
                                                            QIcon::Disabled));
    }
  }
  QLabel::paintEvent(e);
}
